package com.rayboot.footpoint.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.ImageColumns;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;

import com.rayboot.footpoint.R;

public class ImageManage {
	private static final String TAG = "FootprintImageManager";
	private static ImageManage sInstance;
	private String[] projection = { "_id", "_display_name", "_data",
			"date_added" };

	public ImageManage() {
	}

	public static ImageManage getInstance() {
		if (sInstance == null)
			sInstance = new ImageManage();
		return sInstance;
	}

	public Bitmap getImage(Uri uri, Context context) {
		Bitmap bitmap;
		if (uri != null) {
			try {
				Cursor localCursor = context.getContentResolver().query(uri,
						this.projection, null, null, null);
				if (localCursor.getCount() > 0) {
					localCursor.moveToFirst();
					bitmap = BitmapFactory.decodeFile(localCursor.getString(2));
					return bitmap;
				}
				Bitmap localBitmap = BitmapFactory.decodeResource(
						context.getResources(), R.drawable.photo_not_available);
				bitmap = localBitmap;
			} catch (Exception localException) {
				Log.d("FootprintImageManager", "getimage error");
				bitmap = BitmapFactory.decodeResource(context.getResources(),
						R.drawable.photo_not_available);
			}
		} else
			bitmap = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.photo_not_available);
		return bitmap;
	}

	public Bitmap getImage(File file, Context context) {
		Bitmap bitmap = null;
		// if ((file != null) && (file.exists()))
		if (file == null || !file.exists()) {
			return null;
		}
		try {
			bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
			if (bitmap == null) {
				Bitmap localBitmap = BitmapFactory.decodeResource(
						context.getResources(), R.drawable.photo_not_available);
				bitmap = localBitmap;
			}
			if (bitmap == null)
				bitmap = BitmapFactory.decodeResource(context.getResources(),
						R.drawable.photo_not_available);
			return bitmap;
		} catch (Exception e) {
			// TODO: handle exception
			Log.w("FootprintImageManager", e.getMessage(), e);
		}
		return bitmap;
	}

	public void forceWriteToFile(ContentResolver contentResolver,
			Bitmap mPhotoBitmap, String name) {
		// TODO Auto-generated method stub String path =
		String path = ImageCapture.generateFilepath(name);
		byte[] bit = Bitmap2Bytes(mPhotoBitmap);
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(path);
			out.write(bit);
		} catch (Exception e) {
			Log.e(TAG, "Failed to write image", e);
		} finally {
			try {
				out.close();
			} catch (Exception e) {
			}
		}
	}

	public byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		return baos.toByteArray();
	}
}
